package org.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dk.mapper.TbSpecificationOptionMapper;
import org.dk.mapper.TbTypeTemplateMapper;
import org.dk.pageInfo.PageInfo2;
import org.dk.pojo.SpecVo;
import org.dk.pojo.TbSpecificationOption;
import org.dk.pojo.TbSpecificationOptionExample;
import org.dk.pojo.TbTypeTemplate;
import org.dk.service.TbTypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class TbTypeTemplateServiceImpl implements TbTypeTemplateService {

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;
    @Autowired
    private TbSpecificationOptionMapper optionMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;


    @Override
    public PageInfo2<TbTypeTemplate> select(Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        PageInfo<TbTypeTemplate> pageInfo = new PageInfo<>(tbTypeTemplateMapper.selectByExample(null));
        PageInfo2<TbTypeTemplate> pageInfo1 = new PageInfo2();
        pageInfo1.setPageNum(pageInfo.getPageNum());
        pageInfo1.setPageSize(pageInfo.getSize());
        pageInfo1.setTotal(pageInfo.getTotal());
        pageInfo1.setList(pageInfo.getList());
        pageInfo1.setLastPage(pageInfo.getPages());
        return pageInfo1;
    }

    @Override
    public Boolean insert(TbTypeTemplate tbTypeTemplate) {
        int i = tbTypeTemplateMapper.insertSelective(tbTypeTemplate);
        return i>0;
    }

    @Override
    public Boolean delete(Long id) {
        return 0 < tbTypeTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbTypeTemplate selectById(Long id) {
        return tbTypeTemplateMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean updata(TbTypeTemplate tbTypeTemplate) {
        return 0 < tbTypeTemplateMapper.updateByPrimaryKeySelective(tbTypeTemplate);
    }

    @Override
    public List<TbTypeTemplate> selectAll() {
        return tbTypeTemplateMapper.selectByExample(null);
    }

    @Override
    public List<SpecVo> selectTemplateoption(Long id) {
        //查出tb_specification实体
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
        //通过specids查询
        List<Map> maps = JSONObject.parseArray(tbTypeTemplate.getSpecIds(), Map.class);
        List<SpecVo> specVoList = new ArrayList<>();
        for (Map map:maps) {
            //获取specid查询tb_specification_option规格
            Long specId = Long.valueOf((Integer)map.get("id"));
            //获取规格名称
            String specName = (String) map.get("text");
            //模糊查询获取规格
            TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
            criteria.andSpecIdEqualTo(specId);
            List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(tbSpecificationOptionExample);
            //只能返回vo类,方便前台获取
            SpecVo specVo = new SpecVo();
            specVo.setSpecId(specId);
            specVo.setSpecName(specName);
            specVo.setSpecOption(tbSpecificationOptions);
            specVoList.add(specVo);
        }
        return specVoList;
    }
}
